##########################
# 绘制方阵中的最短路径方法
# @Author: MK
# @CreateDate: 2023/10/10
##########################
import time
import turtle


# 传入图（数组）和路径（数组）、最短路径值，绘制路线图
def drawPath(inputGraphArr=[[1, 3, 1], [1, 5, 1], [4, 2, 1]], inputPathArr=[0, 1, 2, 5, 8], pathLength=0):
    # 输入的数组
    graphArr = inputGraphArr
    pathArr = inputPathArr

    # region 设置画板基本信息
    # graphArr = [[2, 1, 6, 4],
    #             [5, 3, 1, 2],
    #             [3, 3, 4, 1],
    #             [4, 2, 3, 5]]
    # 每行方框数量
    RowNum = len(graphArr)
    ColumnNum = len(graphArr[0])
    # 每个框的边长
    EdgeLength = 100
    # 框内字体大小
    VertexFontSize = 20
    # 边框左上角坐标
    EdgeLeftTopX = -ColumnNum * EdgeLength / 2
    EdgeLeftTopY = RowNum * EdgeLength / 2

    # 设置画布大小
    turtle.screensize(1920, 1080)
    # 绘制边框
    turtle.speed(0)  # 动画速度（1-10速度递增），但特殊的0为最快。
    turtle.delay(0)  # 设置延迟（画布刷新间隔），同样影响动画速度。
    # 隐藏光标
    turtle.hideturtle()

    # endregion

    # region 绘制网格边框、网格内的数值

    # 画边框
    # 画横线
    for i in range(RowNum + 1):
        # 抬起画笔
        turtle.penup()
        # 将画笔移动到边框左上角
        turtle.setposition(EdgeLeftTopX, EdgeLeftTopY - EdgeLength * i)
        # 画笔落下
        turtle.pendown()
        # 画横线（初始移动方向就是向右）
        turtle.forward(ColumnNum * EdgeLength)

    # 将画笔方向转向下
    turtle.right(90)
    # 画竖线
    for i in range(ColumnNum + 1):
        # 抬起画笔
        turtle.penup()
        # 将画笔移动到边框左上角
        turtle.setposition(EdgeLeftTopX + EdgeLength * i, EdgeLeftTopY)
        # 画笔落下
        turtle.pendown()
        turtle.forward(RowNum * EdgeLength)

    # 将画笔方向复原（向右）
    turtle.left(90)

    # 填写数字
    NumberLeftTopX = EdgeLeftTopX + EdgeLength / 2
    NumberLeftTopY = EdgeLeftTopY - EdgeLength / 2
    for i in range(len(graphArr)):
        currentNumberY = NumberLeftTopY - EdgeLength * i - VertexFontSize / 2
        for j in range(len(graphArr[0])):
            currentNumberX = NumberLeftTopX + EdgeLength * j + VertexFontSize / 2
            # 移动画笔到对应位置
            turtle.penup()
            turtle.setposition(currentNumberX, currentNumberY)
            # 填写数字
            turtle.pendown()
            turtle.write(str(graphArr[i][j]), align='right', font=('楷体', VertexFontSize))

    # 延迟一小会儿再画路径

    # 绘制最短路径
    # 先移动到起点位置
    offsetX = pathArr[0] % ColumnNum
    offsetY = pathArr[0] / ColumnNum
    turtle.penup()
    turtle.setposition(NumberLeftTopX + offsetX * EdgeLength, NumberLeftTopY - offsetY * EdgeLength)

    # endregion

    # region 绘制前进路线
    # 开始画线
    turtle.speed('slowest')
    turtle.delay(15)
    # 光标形状
    turtle.shape('circle')
    turtle.shapesize(0.5, 0.5, 1)
    turtle.pensize(3)
    turtle.color('green')

    time.sleep(0.6)
    # 使得光标可见
    turtle.showturtle()
    turtle.isvisible()
    time.sleep(0.4)

    turtle.pendown()
    for vertex in pathArr:
        offsetX = vertex % ColumnNum
        offsetY = int(vertex / ColumnNum)
        turtle.goto(NumberLeftTopX + offsetX * EdgeLength, NumberLeftTopY - offsetY * EdgeLength)
    # endregion

    # region 输出路径长度
    turtle.penup()
    turtle.delay(0)
    tempX, tempY = turtle.position()
    turtle.hideturtle()
    turtle.setposition(NumberLeftTopX, NumberLeftTopY - EdgeLength*RowNum)
    turtle.color('black')
    turtle.pendown()
    turtle.write('求得最短距离为'+str(pathLength), font=('楷体', VertexFontSize))
    turtle.penup()
    turtle.setposition(tempX, tempY)
    turtle.color('green')
    turtle.showturtle()
    # endregion

    # 等待退出
    turtle.mainloop()


